// user.ts  
import { defineStore } from "pinia";  
import { reactive } from 'vue';  

export const useUserStore = defineStore("userStore", () => {  
    // 定义用户数据
    const user = reactive({  
        user_id:0,  
        username: "", 
        userpassword:"", 
        gender: "",  
        age: 0,  
        region: "",  
        phone: "",  
        amount: "",  
        email: ""  ,
        status:0,//状态
        balance:0,//余额
    });  

    
    const token = reactive({
        value: '',
    });

    const setToken = (newToken: string) => {
        token.value = newToken;
    };

    // newUser就是login传递的userInfo
    const setUser = (newUser) => {  
        user.user_id = newUser.user_id;  
        user.username = newUser.username;  
        user.gender = newUser.gender;  
        user.age = newUser.age;  
        user.region = newUser.region;  
        user.phone = newUser.phone;   
        user.email = newUser.email;  
        user.status=newUser.status;
        user.balance=newUser.balance;
    };  

    // 清除记录
    const clearUser = () => {  
        user.user_id = 0;  
        user.username = "";  
        user.gender = "";  
        user.age = 0;  
        user.region = "";  
        user.phone = "";  
        user.email = "";  
        user.status=0;
        user.balance=0;//余额
        token.value = '';
    };  

    return { user, setUser, clearUser,setToken };  
}, {  
    // pinia持久化
    persist: true   
});  